import axios from 'axios';
import api from './config.js';

// 获取 AI 响应
export const getAiResponse = async (messages) => {
  try {
    const response = await axios.post(`${api.API_BASE_URL}/ai-chat`, { messages });
    if (response.data && response.data.choices) {
      return response.data.choices[0].message;
    }
  } catch (error) {
    console.error("Error communicating with AI API:", error);
    return null;
  }
};

//获取单词定义
export const getWordDefine = async (word) => {
  try {
    const response = await axios.get(`${api.API_BASE_URL}/api/define/${word}`);
    if (response.data && response.data.success) {
      return response.data.data;  // 包括 word, phonetic, audio, meanings
    } else {
      console.error(response.data.message);
      return null;
    }
  } catch (error) {
    console.error("Error fetching word definition:", error);
    return null;
  }
};




export default getAiResponse;
